#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import _mysql, _mysql_exceptions
def connect_to_base_dicts(db='dbwww58com_58_stats'):
    host = '10.5.20.16'
    port = 58885
    user = 'ec_stat'
    passwd = 'L#c8UM5RGh5C'
    dbh = _mysql.connect(host, user, passwd, db, port)
    return dbh

def query(dbh, q):
    try:
        dbh.query(q)
    except _mysql.MySQLError, e:
        print e
        print q
        return None

    result = dbh.store_result()
    if result is None:  # the sql no response
        return True
    rows = result.fetch_row(0)
    return rows

def get_table_prefix():
    conn = connect_to_base_dicts("dbwww58com_base_dicts")
    q = 'select distinct table_prefix from channel_define'
    r=query(conn,q)
    conn.close()
    return r


if __name__ == '__main__':
    prefix = get_table_prefix()
    conn = connect_to_base_dicts()
    for t in prefix:
        q = 'drop table %s_post_detail_2014' % (t[0])
        query(conn,q)
    conn.close()

